package com.xjh;

import com.xjh.uitl.LoginDao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;

public class Login extends JFrame implements MouseListener {
    JLabel username = new JLabel("用户名");
    JLabel password = new JLabel("密码");

    JTextField usernameText = new JTextField();
    JPasswordField passwordText = new JPasswordField();


    JButton login = new JButton("登录");
    JButton logout = new JButton("退出");

    LoginDao loginDao = new LoginDao();


    public Login() throws SQLException, ClassNotFoundException {
        //初始化窗口
        initJframe();

        //初始化组件
        initView();


        this.setVisible(true);
    }

    private void initView() {

        //设置文字
        Font font = new Font("仿宋", Font.PLAIN, 30);
        Font font2 = new Font("仿宋", Font.PLAIN, 20);
        username.setFont(font);
        password.setFont(font);
        username.setBounds(50, 10, 100, 100);
        password.setBounds(50, 100, 100, 100);


        //设置密码框和文本框
        usernameText.setFont(font2);
        passwordText.setFont(font2);
        usernameText.setBounds(200, 50, 250, 40);
        passwordText.setBounds(200, 130, 250, 40);


        //设置登录和登出按钮
        login.setBounds(200, 200, 100, 30);
        logout.setBounds(350, 200, 100, 30);
        logout.setBorderPainted(false);
        login.setBorderPainted(false);


        login.setFont(new java.awt.Font("仿宋", 1, 15));
        login.setBackground(Color.lightGray);

        logout.setFont(new java.awt.Font("仿宋", 1, 15));
        logout.setBackground(Color.lightGray);

        logout.addMouseListener(this);
        login.addMouseListener(this);


        this.getContentPane().add(login);
        this.getContentPane().add(logout);
        this.getContentPane().add(username);
        this.getContentPane().add(password);
        this.getContentPane().add(usernameText);
        this.getContentPane().add(passwordText);
    }

    private void initJframe() {
        //设置界面的宽高
        this.setSize(603, 380);
        //设置界面的标题
        this.setTitle("用户管理系统");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //取消默认的居中放置，只有取消了才会按照XY轴的形式添加组件
        this.setLayout(null);
        //给整个界面添加键盘监听事件
    }


    @Override
    public void mouseClicked(MouseEvent e) {
        Object source = e.getSource();
        if (source == login) {
            //用户点击了登录按钮
            System.out.println("登录");
            String username = usernameText.getText();
            String password = passwordText.getText();
            try {
                if (loginDao.loginCheck(username, password)) {
                    //登陆成功
                    System.out.println("登录成功");
                    System.out.println(username);
                    System.out.println(password);
                    this.setVisible(false);
                    new Index();
                } else {
                    //登录失败
                    System.out.println("登录失败");
                    System.out.println(username);
                    System.out.println(password);
                    passwordText.setText("");
                }

            } catch (SQLException | ClassNotFoundException ex) {
                throw new RuntimeException(ex);
            }

        } else if (source == logout) {
            //用户点击了退出按钮
            System.out.println("退出");
            System.exit(1);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
